var wxCharts = require('../../../../utils/wxcharts-min.js');
var common = require("../../../../utils/common.js");

var app = getApp();
var ringChart = null;
Page({
    data: {
        dateFrom:"",
        dateTo:"",
        recordType:null,
        recordTypeCN:null,
        recordList:{},
        month:null,
        chartHidden:false,
        dateHidden:false,
        defaultSeries: [{
            name: 'cat1',
            data: 50,
        }],
        typeNum:0,
        category:1,
        isShowCategory:false
    },
    onReady: function (e) {
        
    },
    onLoad:function(options){
        var that = this

        if(app.globalData.recordParam){
            if(app.globalData.recordParam.member.length > 0){
                that.setData({
                    isShowCategory:true
                    })
            }
        }else{
            app.getRecordParam(function(paramRes){
                if(paramRes.status==1){
                if(paramRes.data.member.length > 0){
                    that.setData({
                        isShowCategory:true
                        })
                }
                }else{
                app.errorToGetToken()
                }
            })
        }

        // 页面初始化 options为页面跳转所带来的参数
        that.setData({
            recordType:options.type
            })
        that.setData({month:options.month})
        if(options.type == 'annual'){
            that.setData({
                typeNum:0,
                recordTypeCN:"全年",
                dateHidden:true
                })
        }else if(options.type == 'expense'){
            that.setData({
                typeNum:1,
                recordTypeCN:"支出",
                dateHidden:false
                })
        }else if(options.type == 'income'){
            that.setData({
                typeNum:2,
                recordTypeCN:"收入",
                dateHidden:false
                })
        }else if(options.type == 'card'){
            that.setData({
                typeNum:3,
                recordTypeCN:"卡消费",
                dateHidden:false
                })
        }
        wx.request({
            url: common.apiUrl()+'chart',
            data: {
                token:app.globalData.wangcaiToken,
                recordtype:that.data.typeNum,
                month:that.data.month
            },
            // header: {}, // 设置请求的 header
            success: function(res){
                if(res.data.status==1){
                    that.setData({dateFrom:"< "+res.data.data.daybegin})
                    that.setData({dateTo:res.data.data.dayend+" >"})
                    that.setData({recordList:res.data.data})
                    
                    var windowWidth = 320;
                    try {
                        var result = wx.getSystemInfoSync();
                        windowWidth = result.windowWidth;
                    } catch (e) {
                        console.error('getSystemInfoSync failed!');
                    }

                    if(res.data.data.total.indexOf("0.00") >= 0){
                        that.setData({chartHidden:true})
                        ringChart = new wxCharts({
                            animation: true,
                            canvasId: 'ringCanvas',
                            type: 'pie',
                            extra: {
                                ringWidth: 25
                            },
                            series: that.data.defaultSeries,
                            disablePieStroke: true,
                            width: windowWidth,
                            height: 280,
                            dataLabel: true,
                            legend: true,
                            padding: 0
                        });
                    }else{
                    that.setData({chartHidden:false})
                    ringChart = new wxCharts({
                        animation: true,
                        canvasId: 'ringCanvas',
                        type: 'pie',
                        extra: {
                            ringWidth: 25
                        },
                        series: res.data.data.series,
                        disablePieStroke: true,
                        width: windowWidth,
                        height: 280,
                        dataLabel: true,
                        legend: true,
                        padding: 0
                    });
                }

                

                ringChart.addEventListener('renderComplete', () => {
                    // var exam = ringChart.opts.series[2].color;
                    // console.log(exam);
                });
                setTimeout(() => {
                    ringChart.stopAnimation();
                }, 500);
            }else{
                wx.showModal({
                title: '错误提示',
                content: res.data.msg,
                showCancel:false,
                success: function(res) {
                    if (res.confirm) {
                    app.getToken(function(wangcaiToken){
                        //刷新上一页数据
                        var pages = getCurrentPages();
                        if(pages.length > 1){
                            //上一个页面实例对象
                            var prePage = pages[pages.length - 2];
                            prePage.getData()
                        }
                        wx.navigateBack({
                        delta: 1
                        })
                    })
                    }
                }
                })
            }
        }
    })

    

  },
  recordChange:function(e){
      var that = this;
    that.setData({
        recordType:e.currentTarget.dataset.type
        })
    if(e.currentTarget.dataset.type == 'annual'){
        that.setData({
            typeNum:0,
            recordTypeCN:"全年",
            dateHidden:true
            })
        if(app.globalData.recordParam.member.length > 0){
            that.setData({
                isShowCategory:true
            })
        }
    }else if(e.currentTarget.dataset.type == 'expense'){
        that.setData({
            typeNum:1,
            recordTypeCN:"支出",
            dateHidden:false
            })
        if(app.globalData.recordParam.member.length > 0){
            that.setData({
                isShowCategory:true
            })
        }
    }else if(e.currentTarget.dataset.type == 'income'){
        that.setData({
            typeNum:2,
            recordTypeCN:"收入",
            dateHidden:false
            })
        if(app.globalData.recordParam.member.length > 0){
            that.setData({
                isShowCategory:true
            })
        }
    }else if(e.currentTarget.dataset.type == 'card'){
        that.setData({
            typeNum:3,
            recordTypeCN:"卡消费",
            dateHidden:false,
            isShowCategory:false
        })


    }
    if(e.currentTarget.dataset.month == 'previousmonth'){
        that.setData({month:--that.data.month})
    }else if(e.currentTarget.dataset.month == 'nextmonth'){
        that.setData({month:++that.data.month})
    }
    // console.log(that.data.category+"--------category")
    that.getChart()
  },
  changeMember:function(e){
      var that = this
      if(that.data.category==1){
          that.setData({
            category:2
            })
      }else{
          that.setData({
            category:1
            })
      }
      that.recordChange(e)
  },
  detailType:function(e){
      var that=this
    //   console.log('kind---'+e.currentTarget.dataset.kind)
    //   console.log('name---'+e.currentTarget.dataset.name)
      var name = null
      if(that.data.category==2&&e.currentTarget.dataset.kind<3){
          name='&member='+e.currentTarget.dataset.name
      }else{
          name='&type='+e.currentTarget.dataset.name
      }
      wx.navigateTo({
        url: '/pages/smartlife/wangcai/totalrecord/detailtype/detailtype?kind='+e.currentTarget.dataset.kind+'&month='+that.data.month+name
      })
  },
  getChart: function() {
    var that=this
    // console.log(app.globalData.wangcaiToken+'--------month')
    wx.request({
      url: common.apiUrl()+'chart',
      data: {
          token:app.globalData.wangcaiToken,
          recordtype:that.data.typeNum,
          category:that.data.category,
          month:that.data.month
      },
      // header: {}, // 设置请求的 header
      success: function(res){
          if(res.data.status==1){
            // console.log(res.data.data)
            that.setData({dateFrom:"< "+res.data.data.daybegin})
            that.setData({dateTo:res.data.data.dayend+" >"})
            that.setData({recordList:res.data.data})
            if(res.data.data.total.indexOf("0.00") >= 0){
                that.setData({chartHidden:true})
            }else{
                that.setData({chartHidden:false})
                ringChart.updateData(res.data.data)
            }
        }else{
            app.errorToGetToken()
        }
      }
    })
  },
  getTotalData: function() {
        var that=this
        // console.log("getTotalData")
        that.getChart()
        //刷新上一页数据
        // 因为界面停留在detailtype当前页为5
        var pages = getCurrentPages();
        if(pages.length > 1){
            //上一个页面实例对象
            var prePage = pages[pages.length - 4];
            // 调用wangcai界面的getData方法，刷新wangcai界面数据
            prePage.getData()
        }
    }
});